package demo6;

import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student> {//泛型
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
        /*if (this.age > o.age) {
            return 1;
        } else if (this.age == o.age) {
            return 0;

        } else {
            return -1;
        }*/



    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
/*class  Student {//泛型
    public String name;
    public int age;

    public Student(String name,int age) {
        this.name = name;
        this.age = age;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}*/
//比较器----Comparator
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;

    }
}
class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo((o2.name));
    }
}


public class Test {
    //冒泡排序-------使用比较器的冒泡排序
    public static void bubbleSort(Comparable[] comparables) {
        for(int i = 0;i <comparables.length-1; i++) {
            for(int j = 0;j<comparables.length - i -1;j++){
                if(comparables[j].compareTo(comparables[j+1]) > 0) {
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] =  new Student("zhangsan",21);
        students[1] =  new Student("hisi",16);
        students[2] =  new Student("angwu",19);
        System.out.println("排序前："+Arrays.toString(students));
        bubbleSort(students);
        System.out.println("排序后："+Arrays.toString(students));
    }

    public static void main3 (String[] args) {
        Student[] students = new Student[3];
        students[0] =  new Student("zhangsan",21);
        students[1] =  new Student("hisi",16);
        students[2] =  new Student("angwu",19);
        System.out.println("排序前："+Arrays.toString(students));
        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(students,ageComparator);
        System.out.println("排序后1："+Arrays.toString(students));
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students,nameComparator);
        System.out.println("排序后2："+Arrays.toString(students));

    }
    public static void main2(String[] args) {
        Student student1 = new Student("zhangsan",21);
        Student student2 = new Student("lisi",10);
        //System.out.println(student1<student2);目前这两个学生是无法比较的，所以我们应该根据什么比较，年龄还是姓名？
        //System.out.println(student1.compareTo(student2));
        //AgeComparator ageComparator = new AgeComparator();
        //System.out.println(ageComparator.compare(student1, student2));
        //NameComparator nameComparator = new NameComparator();
        //System.out.println(nameComparator.compare(student1, student2));//相当于第一个姓名的第一个字母与第二个姓名的第一个字母开始比较，值为阿斯格码差值
    }
    public static void main1(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a>b);
    }
}
